<?php
	include 'header.php';
?>
	<?php
		mb_internal_encoding('UTF-8');
		$connect=mysqli_connect('localhost','hristo9153','1111111','sells',3306);
		if(!$connect){
				echo 'connect failed';
		}
		if($_POST){
			$name=trim($_POST['name']);
			$price=trim($_POST['price']);
			$groupid=(int)($_POST['group']);
			$date_delivery=$_POST['date'];
			$ts=time();
			$ts+=3600;
			$date_format='Y-m-d h-i-s';
			$date_order=date($date_format,$ts);
			$error=0;
				if(mb_strlen($name)<4){
					echo '<a><center>"The name is too short or invalid"</center></a><br />';
					$error=1;
				}
				if($price<=0){
					echo '<a><center>"The price is invalid"</center></a><br />';
					$error=1;
				}
				if(!$error){
					$query=('INSERT INTO orders(name_article,groups,date_order,date_delivery,price)
					VALUES("'.$name.'","'.$groupid.'","'.$date_order.'","'.$date_delivery.'","'.$price.'");');
					$save=mysqli_query($connect,$query);
					if(!$save){
						
						echo  mysqli_error($connect);
					}
					else{
						echo '<h3><p>Save in database</p></h5>';	
					}
				}
		}
	?>
	
	<center><h3>Insert date into SQL</h3>
	<form method="POST">
		Name an article<input type="text" name="name">
		Group<select name="group">
			<?php
				foreach($group as $key=>$val){
					echo '<option value="'.$key.'">'.$val.'</option>';
				}
			?>
		</select>
		Date of delivery<input type="date" name="date">
		Price<input type="text" name="price">
		<input type="submit" value="Save">
	</form><br />
	<a href="list_of_orders.php">"List of orders"</a><br />
	<a href="new_account.php">"Created new account"</a><br />
	<a href="session_destroy.php">"EXIT"</a>
	</center>
<?php
	include 'footer.php';
?>
